<template>
  <div class="search-panel">
    <el-row class="m-header-searchbar">
      <el-col :span="3" class="left">
        <img src="//s0.meituan.net/bs/fe-web-meituan/e5eeaef/img/logo.png" alt="美团" />
      </el-col>
      <el-col :span="15" class="center">
        <!-- 搜索框 -->
        <div class="wrapper">
          <el-input
            @input="input"
            v-model="searchWord"
            placeholder="请输入内容"
            @focus="focus"
            @blur="blur"
          ></el-input>
          <el-button type="primary" icon="el-icon-search"></el-button>
          <dl class="hotPlace" v-if="ishotPlace">
            <dt>热门搜索</dt>
            <dd v-for="(item, index) in hotPlaceList" :key="index">
              <router-link :to="{ name: 'goods', params: { name: item } }">{{ item }}</router-link>
            </dd>
          </dl>
          <dl class="searchList" v-if="isSearchList">
            <dd v-for="(item, index) in searchList" :key="index">
              <router-link :to="{ name: 'goods', params: { name: item } }">{{ item }}</router-link>
            </dd>
          </dl>
        </div>
        <p class="suggest">
          <a href="" v-for="(item, index) in suggestList" :key="index">
            <router-link :to="{ name: 'goods', params: { name: item } }">{{ item }}</router-link>
          </a>
        </p>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import api from '@/api/index';

export default {
  data() {
    return {
      searchWord: '',
      isFocus: false,
      hotPlaceList: [],
      searchList: [],
      suggestList: [],
    };
  },
  computed: {
    ishotPlace() {
      return this.isFocus && !this.searchWord;
    },
    isSearchList() {
      return this.isFocus && this.searchWord;
    },
  },
  created() {
    api.searchHotWords().then((res) => {
      this.hotPlaceList = res.data.data;
      this.suggestList = res.data.data;
    });
  },
  methods: {
    focus() {
      this.isFocus = true;
    },
    blur() {
      const that = this;
      setTimeout(() => {
        that.isFocus = false;
      }, 200);
    },
    input() {
      const val = this.searchList;
      api.search().then((res) => {
        console.log(res);
        this.searchList = res.data.data.list.filter((item) => item.indexOf(val) !== -1);
      });
    },
  },
};
</script>

<style lang="scss" scoped>
@import '@/assets/css/public/header/index.scss';
@import '@/assets/css/public/header/search.scss';
</style>
